package com.lang.controller;

import com.lang.entity.Address;
import com.lang.entity.User;
import com.lang.service.AddressService;
import com.lang.service.impl.AddressServiceImpl;
import com.lang.utils.StrUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/*
 * @Date : 2024/3/14 10:47
 * @Created by Lang
 * @ProjectName : LclShop
 */
@WebServlet("/address")
public class AddressServlet extends BaseServlet {
    private AddressService addressService = new AddressServiceImpl();

    // 添加地址
    public String addAddress(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map<String, String[]> map = request.getParameterMap();
            Address address = new Address();
            BeanUtils.populate(address, map);
            address.setA_state(0);
            int res = addressService.addAddress(address);
            System.out.println("添加地址=" + res);
            return StrUtils.FORWARD + "/address?action=showAddress";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 展示地址
    public String showAddress(HttpServletRequest request, HttpServletResponse response) {
        // 根据uid获取地址集合
        User u = (User) request.getSession().getAttribute(StrUtils.lOG_USER);
        List<Address> list = addressService.getAddress(u.getU_id());
        request.setAttribute("addList", list);
        return StrUtils.FORWARD + "/self_info.jsp";
    }

    // 删除地址
    public String deleteAddress(HttpServletRequest request, HttpServletResponse response) {

        String aid = request.getParameter("id");
        int res = addressService.deleteAddressByAid(aid);
        System.out.println("删除地址=" + res);
        return StrUtils.REDIRECT + "/address?action=showAddress";
    }

    // 修改地址
    public String updateAddress(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();
        Address address = new Address();
        BeanUtils.populate(address, map);
        User user = (User) request.getSession().getAttribute(StrUtils.lOG_USER);
        address.setU_id(user.getU_id());
        int res = addressService.updateAddress(address);
        System.out.println("修改地址=" + res);
        return StrUtils.REDIRECT + "/address?action=showAddress";
    }

    // 设置默认地址default
    public String defaultAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("id");
        User user = (User) req.getSession().getAttribute(StrUtils.lOG_USER);
        addressService.defaultAddress(aid,user.getU_id());
        return StrUtils.REDIRECT + "/address?action=showAddress";
    }
}
